import { createFeatureSelector, createSelector, select, Store } from '@ngrx/store';
import { AppState } from '@core/cne.state';
import { LoadState } from './load.models';
import { take } from 'rxjs/operators';
import { toSignal } from '@angular/core/rxjs-interop';

// 选择加载状态的特征选择器
export const selectLoadState = createFeatureSelector<LoadState>('load');

// 选择完整的加载状态对象
export const selectLoad = createSelector(selectLoadState, (state: LoadState) => state);

// 选择是否正在加载的布尔值
export const selectIsLoading = createSelector(selectLoadState, (state: LoadState) => state.isLoading);

// 获取当前加载状态的同步函数
export function getCurrentIsLoading(store: Store<AppState>): boolean {
  // 使用 toSignal 将 Observable 转换为 Signal，避免 subscribe 的副作用
  const isLoadingSignal = toSignal(
    store.pipe(select(selectIsLoading), take(1)),
    { initialValue: true }
  );
  return isLoadingSignal();
}
